<script setup lang="ts">
import type { AddressItem } from '@/types/address'
import { ref, onMounted, computed, onUpdated } from 'vue'
import { getMemberAddressAPI } from '@/services/address'
import { useAddressStore } from '@/stores/modules/address'
import { onShow } from '@dcloudio/uni-app'

const addressStore = useAddressStore()
// 计算出默认收货地址
const selectedAddress = computed(() => {
  return (
    addressStore.selectedAddress ||
    addressList.value.find((item) => item.isDefault)
  )
})

// 获取地址信息
const addressList = ref<AddressItem[]>([])
const getMemberAddressData = async () => {
  const res = await getMemberAddressAPI()
  addressList.value = res.result
  // console.log(res.result)
}

// 切换地址
const onChangeAddress = (item: AddressItem) => {
  // 修改选中的地址
  addressStore.changeSelectedAddress(item)
}

onMounted(() => {
  getMemberAddressData()
})

// 定义子调父方法
const emit = defineEmits<{
  (event: 'close', addressItem?: AddressItem): void
}>()

// 关闭面板
const onClose = () => {
  emit('close', selectedAddress.value!)
}

// 新增地址
const onAddNewAddress = () => {
  // 跳转到新增地址页面
  uni.navigateTo({
    url: '/pagesMember/address-form/address-form',
  })
  emit('close')
}
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <text class="close icon-close" @click="onClose"></text>
    <!-- 标题 -->
    <view class="title">配送至</view>
    <!-- 内容 -->
    <view class="content">
      <view
        class="item"
        v-for="item in addressList"
        :key="item.id"
        @click="onChangeAddress(item)"
      >
        <view class="user">{{ item.receiver }} {{ item.contact }}</view>
        <view class="address">{{ item.address }}</view>
        <text
          :class="
            selectedAddress === item ? 'icon icon-checked' : 'icon icon-ring'
          "
        ></text>
      </view>
      <!-- 设置文字水平和垂直居中 -->
      <view
        :style="{
          'text-align': 'center',
          'align-items': 'center',
        }"
        v-if="addressList.length === 0"
      >
        <view :style="{ 'font-size': '28rpx', color: '#999' }"
          >暂无收货地址
        </view>
      </view>
    </view>
    <view class="footer">
      <view
        v-if="addressList.length > 0"
        class="button primary"
        @click="onClose"
        >确定</view
      >
      <view v-else class="button primary" @click="onAddNewAddress">
        新建地址
      </view>
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 24rpx;
  top: 24rpx;
}

.content {
  min-height: 300rpx;
  max-height: 540rpx;
  overflow: auto;
  padding: 20rpx;
  .item {
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
    position: relative;
  }
  .icon {
    color: #999;
    font-size: 40rpx;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 0;
  }
  .icon-checked {
    color: #27ba9b;
  }
  .icon-ring {
    color: #444;
  }
  .user {
    font-size: 28rpx;
    color: #444;
    font-weight: 500;
  }
  .address {
    font-size: 26rpx;
    color: #666;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #27ba9b;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
